home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_09 / single2 / newtest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-07  |  3.1 KB  |  132 lines

  1. //  Listing 8
  2. //  newtest.cpp 
  3. //
  4. //  Copyright Singleton Systems Ltd 1994 
  5. //
  6.  
  7. #include    "stdafx.h"
  8. #include    <iostream.h> 
  9. #include    <fstream.h>
  10. #include    "newtest.h"
  11. #include    "mobject.h"
  12.  
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char BASED_CODE THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. BEGIN_MESSAGE_MAP(CComdtestApp, CWinApp)
  19.     //{{AFX_MSG_MAP(CComdtestApp)
  20.     ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
  21.     ON_COMMAND(ID_FILE_CLOSE, OnFileClose)
  22.     ON_COMMAND(ID_FILE_PRINT_HEAP_REPORT, 
  23.                OnFilePrintHeapReport)
  24.     //}}AFX_MSG_MAP
  25.     // Standard file based document commands
  26.     ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
  27. END_MESSAGE_MAP()
  28.  
  29.  
  30. //  CComdtestApp object and other globals
  31.  
  32. CComdtestApp NEAR theApp;
  33. ostream_withassign cout; 
  34. int out_file_count = 0;
  35.  
  36. #if defined (_DEBUG)
  37.     CMemoryState start, end, difference;
  38. #endif
  39.  
  40. // CComdtestApp initialization
  41.  
  42. BOOL CComdtestApp::InitInstance()
  43. {
  44. exit_code = 0;
  45.  
  46. // create main MDI Frame window
  47. CMDIFrameWnd * pMainFrame = new CMDIFrameWnd;
  48. if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
  49.     return FALSE;
  50. pMainFrame->ShowWindow(m_nCmdShow);
  51. pMainFrame->UpdateWindow();
  52. m_pMainWnd = pMainFrame;
  53.  
  54. //  Set up the cout window
  55. coutbuf = new winstreambuf;
  56. cout = coutbuf;
  57. cout_window = new ostreamWnd ("cout");
  58. cout_window->SetBufferLines (200);
  59. coutbuf->set_stream_window (cout_window);
  60.  
  61. return TRUE;
  62.  
  63. int CComdtestApp::ExitInstance ()
  64. {
  65. delete coutbuf;
  66.  
  67. #if defined (_DEBUG)
  68. //  Test for memory leakage in MemoryObjects
  69. if (!MemoryObject::MemoryHeapIsEmpty ())
  70.     {
  71.     TRACE0 ("Some MemoryObjects have not been "
  72.             "deleted.  Details are:\n");
  73.     TRACE2 ("%d object(s) remain in %d separate "
  74.             "global memory area(s)\n", 
  75.             MemoryObject::MemoryHeapInUseCount (),
  76.             MemoryObject::HeapBlockInUseCount ()); 
  77.     MemoryObject::DumpAllObjects ();
  78.     set_exit_code (100);
  79.     }
  80.  
  81. //  Test for memory leakage in the standard heap
  82. end.Checkpoint ();
  83. if (difference.Difference (start, end))
  84.     { 
  85.     TRACE0 ("Memory leak detected in ExitInstance!\n");
  86.     difference.DumpStatistics ();
  87.     set_exit_code (100);
  88.     }
  89. #endif
  90. return exit_code;
  91. }
  92.  
  93. // App command to run the dialog
  94. void CComdtestApp::OnAppAbout()
  95. {
  96.     CDialog aboutDlg (IDD_ABOUTBOX);
  97.     aboutDlg.DoModal();
  98. }
  99.  
  100. void CComdtestApp::OnFileOpen ()
  101. //  A convenient hook to get the test to run
  102. {
  103. TRY
  104.     {DoTest ();}
  105. CATCH (CException, e)
  106.     {               
  107.     AfxMessageBox ("An exception occurred while "
  108.                    "running CComdtestApp::DoTest()");
  109.     }    
  110. END_CATCH   
  111. }
  112.  
  113.  
  114. void CComdtestApp::OnFilePrintHeapReport()
  115.     {FilePrintHeapReport();}
  116.     
  117. //  static function to do all the work.  May also be 
  118. //  called from inside another static function.
  119. void CComdtestApp::FilePrintHeapReport(char * msg)
  120. CString out_file_name = "hprpt"; 
  121. char buf [20];
  122. out_file_name += _itoa (out_file_count++, buf, 10);
  123. out_file_name += ".txt";
  124. cout << "FilePrintHeapReport() dumping to " 
  125.      << out_file_name << endl;
  126. ofstream fout (out_file_name);
  127. MemoryObject::PrintHeapReport (fout, msg);
  128. fout.close ();
  129. }
  130.